/*
 * @Author: 一路向阳 tt_sunzhenfeng@163.com
 * @Date: 2024-03-11 20:51:37
 * @LastEditors: 一路向阳 tt_sunzhenfeng@163.com
 * @LastEditTime: 2024-03-11 21:01:28
 * @FilePath: \utils_react_manager\server\utils\useTimeDiff.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const useTimeDiff = (startTime, endTime) => {
  if (startTime == null || endTime == null) return {};

  const start = typeof startTime === 'number' ? startTime : new Date(startTime).getTime();
  const end = typeof endTime === 'number' ? endTime : new Date(endTime).getTime();

  const diff = end - start; // 返回时间差的毫秒数

  // 转换为天、小时、分钟和秒
  var days = Math.floor(diff / (1000 * 60 * 60 * 24));
  var hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((diff % (1000 * 60)) / 1000);

  return {
    days,
    hours,
    minutes,
    seconds
  }
}

module.exports = useTimeDiff;